package com.ita.fileutil;


import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Queue;


public class FileReaderT implements Runnable{
	
	private Queue<Student> queue;

	public FileReaderT(Queue<Student> queue) {
		super();
		this.queue = queue;
	}



	@Override
	public void run() {
		// TODO Auto-generated method stub
		ObjectInputStream objectInputStream;
		try {
			objectInputStream = new ObjectInputStream(new FileInputStream("./files/students.txt"));
			
			Student student = null;

			while((student = (Student)objectInputStream.readObject()) != null){	
				synchronized (queue) {
					if(queue.size() <= 10){
						System.out.println(student.getSname() + "---" + student.getAge());
						queue.add(student);
						queue.notify();
					}else {
						queue.notify();
						queue.wait();
					}
				}
			}
			objectInputStream.close();
		} catch (EOFException e) {
			// TODO: handle exception
			System.out.println("File end");
			queue.notify();
		}catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
